#include"jiekou.h"
MYSQL* initDB(const char *host ,const char *user,const char*passwd,const char *db)
{
    MYSQL *mysql= mysql_init(NULL);
    MYSQL *ret=mysql_real_connect(mysql,host,user,passwd,db,0,NULL,0);
    if(ret==NULL)
    {
        fprintf(stderr,"%s%s\n","mysql_real_connect",mysql_error(mysql));

    }
    return mysql;
}
void getMD5(const char *filename,char *result)
{
    MD5_CTX md5;
    MD5_Init(&md5);
    int fd=open(filename,O_RDWR);
    ERROR_CHECK(fd,-1,"open");
    char buf[4096];
    while(1)
    {
        bzero(buf,sizeof(buf));
        ssize_t sret=read(fd,buf,sizeof(buf));
        if(sret==0)
        {
            break;
        }
        MD5_Update(&md5,buf,sret);
    }
    unsigned char md[16];
    MD5_Final(md,&md5);
    char md5_str[33]={0};
    for(int i=0;i<16;i++)
    {
        char temp[3]={0};
        sprintf(temp,"%02x",md[i]);
        strcat(md5_str,temp);
    }
    close(fd);
    strncpy(result,md5_str,33);
}
int execDB(const char*sql,MYSQL*db)
{
    int qrey=mysql_query(db,sql);
    if(qrey!=0)
    {
        printf("数据库语句不正确2\n");
        return-1;
    }
    return 0;
}
int selectUser1(const char *sql,MYSQL *db,user_t *user)
{
    int query=mysql_query(db,sql);
    if(query!=0)
    {
        printf("数据库语句不正确1\n");                   
        return-1;
    }
    MYSQL_RES *res=mysql_store_result(db);
    if(res==NULL){
        printf("存储执行结果失败\n");
        return 0;
    }
    unsigned int cols=mysql_num_fields(res);
    MYSQL_FIELD *fields = mysql_fetch_fields(res);
    int idx_id = -1;
    int idx_username = -1;
    int idx_salt = -1;
    int idx_encrypted_pass = -1;
    int idx_is_delete = -1;

    for (int i = 0; i < cols; i++) {
        if (strcmp(fields[i].name, "id") == 0) idx_id = i;
        else if (strcmp(fields[i].name, "username") == 0) idx_username = i;
        else if (strcmp(fields[i].name, "salt") == 0) idx_salt = i;
        else if (strcmp(fields[i].name, "encrypted_password") == 0) idx_encrypted_pass = i;
        else if (strcmp(fields[i].name, "is_deleted") == 0) idx_is_delete = i;
    }
    int count=0;
    MYSQL_ROW row;
    while((row=mysql_fetch_row(res))!=NULL){
        bzero(&user->user[count], sizeof(user_info_t));
        if(count >= 1024) break;
        if(idx_id!=-1&&row[idx_id]!=NULL){
            user->user[count].id=atoi(row[idx_id]);

        }
         if(idx_username!=-1&&row[idx_username]!=NULL){
            strncpy(user->user[count].username,row[idx_username],sizeof(user->user[count].username)-1);

        }
        if(idx_salt!=-1&&row[idx_salt]!=NULL){
            strncpy(user->user[count].salt,row[idx_salt],sizeof(user->user[count].salt)-1);

        }
         if(idx_encrypted_pass!=-1&&row[idx_encrypted_pass]!=NULL){
            strncpy(user->user[count].encrypted_password,row[idx_encrypted_pass],sizeof(user->user[count].encrypted_password)-1);
        }
         if(idx_is_delete!=-1&&row[idx_is_delete]!=NULL){
            user->user[count].is_deleted=atoi(row[idx_is_delete]);

        }
        count++;

    }
    mysql_free_result(res);
    return count;
}
void closeDB(MYSQL *db)
{
    mysql_close(db);
}
int selectFile1(const char *sql,MYSQL*db,file_t *file)
{
    //1 执行mysql_query
   int qury=mysql_query(db, sql);
   if(qury!=0){
    printf("执行sql语句失败in selectFile\n");
    return -1;
   }
   //2. 执行mysql_store_result 存储查询到的结果
   MYSQL_RES *res=mysql_store_result(db);
   if(res==NULL){
    printf("存储执行结果失败in selectFile\n");
    return 0;
   }
   // 3.获取查询到的列数，以及列的信息
   unsigned int cols=mysql_num_fields(res);
   MYSQL_FIELD *fields=mysql_fetch_fields(res);
   //4.构建列名到索引
   int idx_id=-1;
   int idx_username=-1;
   int idx_filename=-1;
   int idx_path=-1;
   int idx_md5=-1;
   int idx_parent_id=-1;
   int idx_is_delete=-1;
   int idx_md5_path=-1;
   int idx_type=-1;

   for(int i=0;i<cols;i++){
    if(strcmp(fields[i].name,"id")==0) idx_id=i;
    else if(strcmp(fields[i].name,"username")==0) idx_username=i;
    else if(strcmp(fields[i].name,"filename")==0) idx_filename=i;
    else if(strcmp(fields[i].name,"path")==0) idx_path=i;
    else if(strcmp(fields[i].name,"md5")==0) idx_md5=i;
    else if(strcmp(fields[i].name,"parent_id")==0) idx_parent_id=i;
    else if(strcmp(fields[i].name, "md5_path")==0) idx_md5_path=i;
    else if(strcmp(fields[i].name,"is_deleted")==0) idx_is_delete=i;
    else if(strcmp(fields[i].name,"type")==0) idx_type=i;
   }
   // 5. 按照行获取结果
   int count=0;
   MYSQL_ROW row;
   while((row=mysql_fetch_row(res))!=NULL){
    bzero(&file->file[count], sizeof(file_info_t));
    if(count >= 1024) break;
    if(idx_id!=-1&&row[idx_id]!=NULL){
        file->file[count].id=atoi(row[idx_id]);
    }
    if(idx_username!=-1&&row[idx_username]!=NULL){
        strncpy(file->file[count].username,row[idx_username],sizeof(file->file[count].username)-1);
    }
    if(idx_filename!=-1&&row[idx_filename]!=NULL){
        strncpy(file->file[count].filename,row[idx_filename],sizeof(file->file[count].filename)-1);
    }
    if(idx_path!=-1&&row[idx_path]!=NULL){
        strncpy(file->file[count].path,row[idx_path],sizeof(file->file[count].path)-1);
    }
    if(idx_md5!=-1&&row[idx_md5]!=NULL){
        strncpy(file->file[count].md5,row[idx_md5],sizeof(file->file[count].md5)-1);
    }
    if(idx_parent_id!=-1&&row[idx_parent_id]!=NULL){
        file->file[count].parent_id=atoi(row[idx_parent_id]);
    }
    if(idx_is_delete!=-1&&row[idx_is_delete]!=NULL){
        file->file[count].is_deleted=atoi(row[idx_is_delete]);
    }
    if(idx_md5_path!=-1&&row[idx_md5_path]!=NULL){
        strncpy(file->file[count].md5_path,row[idx_md5_path],sizeof(file->file[count].md5_path)-1);
    }
    if(idx_type!=-1&&row[idx_type]!=NULL&&row[idx_type][0]!='\0'){
         file->file[count].type=(row[idx_type][0]);
    }
        count++;
   }
        mysql_free_result(res);
        return count;
}
